from .utils import extract_list, build_list_to_json
import pytest


# 单元测试
def test_extract_list_with_valid_metadata():
    input_string = """  
    Some random text
    metadata-start
line1
line2
line3
metadata-end
    More random text """
    expected_output = ["line1", "line2", "line3"]
    result = extract_list(input_string)
    assert result == expected_output


def test_extract_list_with_no_metadata():
    input_string = """
    Some random text
    No metadata here
    More random text
    """
    expected_output = []
    assert extract_list(input_string) == expected_output


def test_extract_list_with_incomplete_metadata():
    input_string = """
    Some random text  
    metadata-start
    line1
    line2
    """
    expected_output = []
    assert extract_list(input_string) == expected_output


def test_extract_list_with_empty_metadata():
    input_string = """  
    metadata-start
metadata-end
    """
    expected_output = []
    assert extract_list(input_string) == expected_output


def test_extract_list_with_trailing_newline():
    input_string = """  
    metadata-start
line1
line2


metadata-end
    """
    expected_output = ["line1", "line2"]
    assert extract_list(input_string) == expected_output


def test_basic_conversion():
    items = ["name: John Doe", "age: 30", "is_student: False"]
    expected = {"name": "John Doe", "age": 30, "is_student": "False"}
    assert build_list_to_json(items) == expected


def test_whitespace_handling():
    items = [" name : John Doe ", " age : 30 ", " is_student : False "]
    expected = {"name": "John Doe", "age": 30, "is_student": "False"}
    assert build_list_to_json(items) == expected


def test_integer_values():
    items = ["age: 30", "height: 175"]
    expected = {"age": 30, "height": 175}
    assert build_list_to_json(items) == expected


def test_float_values():
    items = ["weight: 70.5", "height: 175.0"]
    expected = {"weight": 70.5, "height": 175.0}
    assert build_list_to_json(items) == expected


def test_mixed_values():
    items = ["name: John Doe", "age: 30", "height: 175.5", "is_student: False"]
    expected = {"name": "John Doe", "age": 30, "height": 175.5, "is_student": "False"}
    assert build_list_to_json(items) == expected


def test_empty_list():
    items = []
    expected = {}
    assert build_list_to_json(items) == expected


def test_invalid_input():
    items = ["name: John Doe", "invalid_entry", "age: 30"]
    with pytest.raises(ValueError):
        build_list_to_json(items)
